package com.nursinghome.common.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.nursinghome.bean.PageFilter;
import com.nursinghome.bean.Reservation;
import com.nursinghome.common.mapper.ReservationMapper;
import com.nursinghome.common.service.ReservationService;
import com.nursinghome.common.service.ServiceService;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

/**
* @author 程硕
* @createDate 2025-04-07 01:10:33
*/
@Service
public class ReservationServiceImpl implements ReservationService{
    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private ServiceService serviceService;
    /*
     * 生成雪花算法的id
     * 生成对应的服务详细
     * */
    @Override
    public Boolean insert(Reservation reservation) throws NurisingHomeException {
        // 算法算法生成唯一id 1910337022447456256
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        String id = snowflake.nextIdStr();
        reservation.setId(id);
        // 判断该护工当前时间有没有其他服务
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String endTime = sdf.format(reservation.getEndTime());
        String startTime = sdf.format(reservation.getStartTime());
        Long hour = DateUtil.between(reservation.getStartTime(), reservation.getEndTime(), DateUnit.HOUR) ;

        Reservation byDate = reservationMapper.selectByDate(reservation.getMid(), startTime, endTime);
        if (byDate != null){
            throw new NurisingHomeException("该护工当前时间段已经有服务了");
        }
        com.nursinghome.bean.Service service = serviceService.selectById(reservation.getServiceId());
        // 计算总金额
        reservation.setTotalPrice(service.getPrice().multiply(new BigDecimal(hour)));
        // 生成服务详情
        return reservationMapper.insert(reservation) > 0;
    }

    @Override
    public Boolean update(Reservation reservation) throws NurisingHomeException {
        // 查询订单是否存在
        Reservation reservationOld = reservationMapper.selectById(reservation.getId());
        if (reservationOld == null){
            throw new NurisingHomeException("订单不存在");
        }
        // 判断该护工当前时间有没有其他服务
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String endTime = sdf.format(reservation.getEndTime());
        String startTime = sdf.format(reservation.getStartTime());
        Reservation byDate = reservationMapper.selectByDate(reservation.getMid(), startTime, endTime);
        if (byDate != null){
            throw new NurisingHomeException("该护工当前时间段已经有服务了");
        }
        return reservationMapper.update(reservation) > 0;
    }
    @Override
    public Boolean updateByState(Reservation reservation) throws NurisingHomeException {
        // 查询订单是否存在
        Reservation reservationOld = reservationMapper.selectById(reservation.getId());
        if (reservationOld == null){
            throw new NurisingHomeException("订单不存在");
        }
        return reservationMapper.update(reservation) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(String id) throws NurisingHomeException {
        // 查询订单是否存在
        Reservation reservationOld = reservationMapper.selectById(id);
        if (reservationOld == null){
            throw new NurisingHomeException("订单不存在");
        }

        return reservationMapper.update(
                new Reservation(id,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        3,
                        null,
                        null,
                        null)
        ) > 0;
    }

    @Override
    public Reservation selectById(String id) {
        return reservationMapper.selectById(id);
    }

    @Override
    public PageFilter<Reservation> selectAll(String pageNum, String pageSize, Reservation reservation) {
        reservation.setPageNum(pageNum);
        reservation.setPageSize(pageSize);
        List<Reservation> list = reservationMapper.selectAll(reservation);
        return reservation.getPageFilter(reservation, list);
    }

    @Override
    public List<Reservation> selectAllByCondition(Reservation reservation) {
        return reservationMapper.selectAll(reservation);
    }
}
